//获取应用实例
const app = getApp()
const computer = require('../../utils/computer.js')

Page({
  data: {
    welcome:"对不起，浪费您宝贵的时间了。",
    tips:"请您先走",
    node: [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]],
    nextStepper:0,//0-user,1-computer
    gameOver:false
  },
  onLoad: function () {

  },
  onItemTap: function(e) {
    let stepper = this.data.nextStepper;
    if (stepper != app.USER_VALUE) {
      return;
    }
    let index = parseInt(e.target.id);
    let row = parseInt(index / 3);
    let column = index % 3;
    if (this.data.node[row][column] === -1){
      
      this.data.node[row][column] = stepper;

      let tips, gameOver;
      if (this.checkWin(stepper)) {
        tips = "恭喜您赢了！";
        gameOver = true;
      }else if(this.isFullChess()) {
        tips = "平局";
        gameOver = true;
      }else{
        tips="到电脑走了";
        gameOver = false;
      }
      let theThis = this;
      this.setData({
        node: this.data.node,
        nextStepper: 1 - app.USER_VALUE,
        tips,
        gameOver
      },function(){
        if (!gameOver) {
          let computerExecutor = new computer.Computer(theThis.data.node);
          console.log(computerExecutor);
          let bsetMove = computerExecutor.findCompMove();
          console.log("bsetMove", bsetMove)
          if(bsetMove) {
            theThis.autoNextStep(bsetMove.position);
          }else{
            theThis.autoNextStep();
          }
          
        }
      })       
    }
  },
  checkWin(stepper) {//检查谁赢了,这步谁走的
    
    let isRowComplete, isColumnComplete, isDiagonalComplete;
    for(let i=0;i<3;i++) {
      isRowComplete = true;
      isColumnComplete = true;

      if(i<2){
        isDiagonalComplete = true;
      }

      for(let j=0;j<3;j++) {
        if (isRowComplete && this.data.node[i][j] != stepper) {//横
          isRowComplete = false;
        }
        if (isColumnComplete && this.data.node[j][i] != stepper) {//竖
          isColumnComplete = false;
        }
        if (i < 2 && isDiagonalComplete && this.data.node[j + 2 * i * (1 - j)][j] != stepper) {//斜
          isDiagonalComplete = false;
        }
      }
      if (isRowComplete || isColumnComplete || isDiagonalComplete) {
        return true;
      }
      
    }
        
   return false;
  },
  isFullChess() {//检查棋盘是否满了
    for(let i=0;i<3;i++) {
      for(let j=0;j<3;j++) {
        if(this.data.node[i][j]===-1){
          return false;
        }
      }
    }
    return true;
  },
  autoNextStep(position) {//自动下一步
    let stepper = 1 - app.USER_VALUE;
    if(position !== undefined) {
      let row = parseInt(position / 3);
      let column = position % 3;
      this.data.node[row][column] = stepper;
      let tips;
      let gameOver;
      if (this.checkWin(stepper)) {
        tips = "很遗憾电脑赢了！";
        gameOver = true;
      } else if (this.isFullChess()) {
        tips = "平局";
        gameOver = true;
      } else {
        tips = "到您走了";
        gameOver = false;
      }

      this.setData({
        node: this.data.node,
        nextStepper: app.USER_VALUE,
        tips,
        gameOver
      })
      return;
    }
   
    for (let i = 0; i < 3; i++) {
      for (let j = 0; j < 3; j++) {
        if (this.data.node[i][j] === -1) {
          this.data.node[i][j] = stepper;
          let tips;
          let gameOver;
          if (this.checkWin(stepper)) {
            tips = "很遗憾电脑赢了！";
            gameOver = true;
          } else if (this.isFullChess()) {
            tips = "平局";
            gameOver = true;
          } else {
            tips = "到您走了";
            gameOver = false;
          }

          this.setData({
            node: this.data.node,
            nextStepper: app.USER_VALUE,
            tips,
            gameOver
          })
          return;
        }
      }
    }
  },
   

  reset() {
    this.setData({
      tips: "请您先走",
      node: [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]],
      nextStepper: 0,//0-user,1-computer
      gameOver: false
    })
  }


})
